package com.zkaler.commons.session;


import com.zkaler.common.constants.global.CookieConstants;
import com.zkaler.commons.utils.generate.UUIDGenerater;
import com.zkaler.commons.utils.http.CookieUtils;
import com.zkaler.commons.utils.redis.RedisUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.support.collections.RedisMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RedisSession {



	/**
	 * 获取session
	 * @param req
	 * @param res
     * @return
     */
	public static RedisMap<String, Object> getSession(HttpServletRequest req, HttpServletResponse res) {
		String sessionId = CookieUtils.getValue(req, CookieConstants.DEFAULT_SESSION_COOKIE_NAME);
		if(StringUtils.isBlank(sessionId)){
			sessionId=(String)req.getAttribute(CookieConstants.DEFAULT_SESSION_COOKIE_NAME);
		}
		if (StringUtils.isBlank(sessionId)) {
			sessionId = UUIDGenerater.getUpperShortUUID();
			req.setAttribute(CookieConstants.DEFAULT_SESSION_COOKIE_NAME,sessionId);
			CookieUtils.setValue(req, res, CookieConstants.DEFAULT_SESSION_COOKIE_NAME,sessionId,CookieConstants.DEFAULT_COOKIE_DEMON);
			RedisUtils.redisMap(sessionId).put("_time", System.currentTimeMillis());
		}
        RedisUtils.expired(sessionId, CookieConstants.DEFAULT_SESSION_SAVE_TIME);
        return RedisUtils.redisMap(sessionId);
	}


	/**
	 * 获取session属性
	 * @param req
	 * @param res
	 * @param key
     * @return
     */
	public static Object getSessionAttribute(HttpServletRequest req, HttpServletResponse res, String key) {
		return getSession(req, res).get(key);
	}

	/**
	 * 设置session属性
	 * @param req
	 * @param res
	 * @param key
     * @param value
     */
	public static void setSessionAttribute(HttpServletRequest req, HttpServletResponse res, String key, Object value) {
		getSession(req, res).put(key, value);
	}

	/**
	 * 清除session
	 * @param req
	 * @param res
     */
	public static void clear(HttpServletRequest req, HttpServletResponse res) {
		getSession(req, res).clear();
		String sessionId = CookieUtils.getValue(req, CookieConstants.DEFAULT_SESSION_COOKIE_NAME);
		if (StringUtils.isNotBlank(sessionId)) {
			RedisUtils.del(sessionId);
		}
		CookieUtils.clear(req, res, CookieConstants.DEFAULT_SESSION_COOKIE_NAME);
	}

	/**
	 * 获取session剩余存活时间
	 * @param req
	 * @return
     */
	public static long getSessionLive(HttpServletRequest req) {
		String sessionId = CookieUtils.getValue(req, CookieConstants.DEFAULT_SESSION_COOKIE_NAME);
		if (StringUtils.isNotBlank(sessionId)) {
            return RedisUtils.getTtl(sessionId);
        } else {
            return 0l;
		}
	}

	/**
	 * 重置session存活时间
	 * @param req
	 * @param res
     */
	public static void resetLiveTime(HttpServletRequest req, HttpServletResponse res) {
		String sessionId = CookieUtils.getValue(req, CookieConstants.DEFAULT_SESSION_COOKIE_NAME);
		if (StringUtils.isNotBlank(sessionId)) {
            CookieUtils.setValue(req, res, CookieConstants.DEFAULT_SESSION_COOKIE_NAME,sessionId,CookieConstants.DEFAULT_COOKIE_DEMON);
			RedisUtils.expired(sessionId, CookieConstants.DEFAULT_SESSION_SAVE_TIME);
		}
	}
}
